import math

allowed_scores = {}
allowed_scores[0] = set()
allowed_scores[2] = set()
allowed_scores[3] = set()
allowed_scores[4] = set()

allowed_scores[2].add('Adamic-Adar')
allowed_scores[2].add('Jaccard')
allowed_scores[2].add('Common neighbors')

for i in (0, 2, 3,4):
    allowed_scores[i].add('Weighted category overlap')
    allowed_scores[i].add('Category overlap')

    allowed_scores[i].add('Diversity place overlap')
    allowed_scores[i].add('Entropy place overlap')
    allowed_scores[i].add('Weighted place overlap')
    allowed_scores[i].add('Place overlap')

    allowed_scores[i].add('Weighted geographic distance')
    allowed_scores[i].add('Geographic distance')

    allowed_scores[i].add('Preferential attachment')
    allowed_scores[i].add('User entropy')

    allowed_scores[i].add('MaxFlow')
    allowed_scores[i].add('PropFlow')


def getName(x):
    if 'AdamicAdar' in x:
        return 'Adamic-Adar'

    if 'MaxFlow' in x:
        return x
    if 'PropFlow' in x:
        return x

    if 'DiversityCategoryOverlapScoreComputer' in x:
        return 'Diversity category overlap'
    if 'EntropyCategoryOverlapScoreComputer' in x:
        return 'Entropy category overlap'
    if 'WeightedCategoryOverlap' in x:
        return 'Weighted category overlap'
    if 'CategoryOverlap' in x:
        return 'Category overlap'

    if 'DiversityPlaceOverlapScoreComputer' in x:
        return 'Diversity place overlap'
    if 'EntropyPlaceOverlapScoreComputer' in x:
        return 'Entropy place overlap'
    if 'WeightedPlaceOverlap' in x:
        return 'Weighted place overlap'
    if 'PlaceOverlap' in x:
        return 'Place overlap'

    if 'WeightedGeographicDistanceScoreComputer' in x:
        return 'Weighted geographic distance'
    if 'GeographicDistanceScoreComputer' in x:
        return 'Geographic distance'

    if 'Jaccard' in x:
        return 'Jaccard'

    if 'NeighborOverlapScoreComputer' in x:
        return 'Common neighbors'

    if 'PreferentialAttachmentScoreComputer' in x:
        return 'Preferential attachment'

    if 'RandomScoreComputer' in x:
        return 'Random'

#    if 'SocialDistanceScoreComputer' in x:
#        return 'Shortest path'

    if 'UserEntropyScoreComputer' in x:
        if 'First' in x:
            return None
        if 'Second' in x:
            return None
        return 'User entropy'

    if 'katz' in x:
        return 'Katz'

    return None

def ecdf(seq,normed=False,inverse=False):
    import pylab as plt
    import numpy as np
    print normed, inverse
    pdf = {}
    for x in seq:
        if not x in pdf:
            pdf[x] = 0
        pdf[x] += 1

    data = sorted((x,pdf[x]) for x in pdf)
    
    x,y = [],[]
    t = 0
    x.append(data[0][0])
    y.append(data[0][1])

    for val,freq in data[1:]:
        x.append(val)
        y.append(y[-1]+freq)

    if normed:
        tot = y[-1]
        y = map(lambda i: 1.0*i/tot, y)

    if inverse:
        y = map(lambda i: 1.0-i,y)

    return x,y

def log_ccdf(values,log=False):
    import pylab as plt
    import numpy as np
    if log:
        values = [v for v in values if v > 0]
    print min(values), max(values)
    l1,l2 = min(values),max(values)
    if log:
        l1,l2 = map(math.log10,(l1,l2))
    l1 = math.floor(l2)
    l2 = math.ceil(l2)
    bins = np.linspace(l1,l2, 100)
    if log:
        bins = 10**bins
    pdf,bins,x = plt.hist(values,bins=bins,cumulative=False,normed=False)
    plt.close()

    c = [pdf[0]]
    for j in range(1,len(pdf)):
        t = c[j-1]
        c.append(t+pdf[j])

    total = c[-1]
    c = map(lambda x: 1.0*x/total, c)
    return bins[:-1],c
